package com.example.springboot.utils.file;

import org.dom4j.*;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

/**
 * xml解析工具
 * 
 */
public class XmlFileUtil {
	private static Document doc;

	public static Document loadFileByContent(String fileContent) throws DocumentException {
		doc = DocumentHelper.parseText(fileContent);
		return doc;
	}

	public static Document loadFileByPath(String FilePath) throws DocumentException {
		File file = new File(FilePath);
		if (file.exists()) {
			SAXReader reader = new SAXReader();
			doc = reader.read(file); // 读取一个xml的文件
		}
		return doc;
	}

	public static Document loadFile(File file) throws DocumentException {
		SAXReader reader = new SAXReader();
		doc = reader.read(file); // 读取一个xml的文件
		return doc;
	}

	public static List selectNodes(Node node, String tiaojian) {
		List<Node> nodelist = node.selectNodes(tiaojian);
		return nodelist;
	}

	public static Element rootElement(Document doc) {
		Element ele = doc.getRootElement();
		return ele;
	}

	public static Element element(Element m, String name) {
		Element ele = m.element(name);
		return ele;
	}

	public static List<Element> elements(Element m, String name) {
		List<Element> elist = m.elements(name);
		return elist;
	}

	public static List<Element> elements(Element m) {
		List<Element> elist = m.elements();
		return elist;
	}

	public static String attributeValue(Element m, String attriName) {
		String val = m.attributeValue(attriName);
		return val;
	}

	public static List<String> attributeValues(Element m) {
		List<String> vals = m.attributes();
		return vals;
	}

	public static void main(String[] args) {
		try {
			Document doc = XmlFileUtil
					.loadFileByPath("E:\\workspace_work\\netbox-doc\\BCP\\导出实例\\145-748836377-410100-410100-1462938002-10000\\GAB_ZIP_INDEX.xml");
			Element e = XmlFileUtil.rootElement(doc);
			// List<Element> list = XmlFileOpe.elements(e);
			Element node = (Element) e.selectSingleNode("/MESSAGE/DATASET/DATA/DATASET[@name='WA_COMMON_010013']/DATA");
			// List<Element> list = XmlFileOpe.selectNodes(doc,
			// "/MESSAGE/DATASET/DATA/DATASET[@name='WA_COMMON_010013']");
			System.out.println(((Element) node.selectSingleNode("ITEM[@key='I010032']")).attributeValue("key"));
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
}
